package com.appkubes.fintech.restful.application.credit.controller;

import com.appkubes.fintech.common.response.Response;
import com.appkubes.base.core.entity.vo.UserKeyVO;
import com.appkubes.fintech.core.po.FintechCredit;
import com.appkubes.fintech.restful.application.credit.entity.dto.CreditDTO;
import com.appkubes.fintech.restful.application.credit.service.CreditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Rongjie
 * @date 2020-01-04
 */
@Api(tags = "信用模块")
@RequestMapping("/credit")
@RestController
@Slf4j
public class CreditController {

    @Autowired
    private CreditService creditService;

    /**
     * 获取用户的信用额度
     *
     * @param userKeyVO
     * @param bindingResult
     * @return
     */
    @ApiOperation("获取用户的信用额度")
    @GetMapping("/get")
    public Response<CreditDTO> getUserCredit(@Valid UserKeyVO userKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        FintechCredit credit = creditService.getCredit(userKeyVO.getUserKey());
        CreditDTO creditDTO = new CreditDTO();
        BeanUtils.copyProperties(credit, creditDTO);
        creditDTO.setFrozenAmount(credit.getForzenAmount());
        try {
            creditDTO.setRefuseLeftDays(creditService.refuseExpireLeftDays(userKeyVO.getUserKey()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.success(creditDTO);
    }


}
